using System;
using System.Data;
using System.Data.SqlClient;
using PHSRAG.Utility;
using BO = PHSRAG.Insight21.BusinessObjects;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using GenerateKey = PHSRAG.Insight21.BusinessObjects.ModuleKey;

namespace PHSRAG.Insight21.Admin
{
	/// <summary>
	/// The object represents an HR Department within Admin module. It has methods
	/// to load the department, and also methods to persist, remove and retrieve itself
	/// from session.
	/// </summary>
	public class HRDepartment : BO.BusinessObject
	{
		#region Constants
		private static string HRDepartmentKey = GenerateKey.MakeKey("HRDepartmentKey_6309F151-091E-47d1-922B-4BABE6D62FD9",GenerateKey.Admin);
		private const string GetAdminHRDepartmentSprocName = "GetAdminHRDepartment";
		#endregion

		#region Constructor
		/// <summary>
		/// Instantiates the data container.
		/// </summary>
		/// <param name="cachePolicy">Application's cache policy.</param>
		/// <param name="securityPolicy">Application's security policy.</param>
		public HRDepartment(ICachePolicy cachePolicy) : base(cachePolicy)
		{
			DataContainer = new DataSet();
		}
		#endregion

		#region Base Class Overrides
		/// <summary>
		/// Puts this instance to session using a specified key.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(HRDepartmentKey, this);
		}
		/// <summary>
		/// Removes this instance from session.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(HRDepartmentKey);
		}
		/// <summary>
		/// Loads the data container with data from repository.
		/// </summary>
		/// <param name="connectionString">The connection string to the data source.</param>
		/// <param name="args">arguments to be passed to the stored procedure.</param>
		public override void Load(string connectionString, params object[] args)
		{
			DataContainer = DataAccess.GetDataSet(connectionString, GetAdminHRDepartmentSprocName,
				DataAccess.BuildSqlParameterArray(
				"@hrDepartmentID", SqlDbType.Int, args[0]));
		}
		#endregion

		#region Class Methods
		/// <summary>
		/// Persists changes to the HR department management status type to the database.
		/// </summary>
		/// <param name="connectionString">The connection string to the data source.</param>
		/// <param name="args">arguments to be passed to the stored procedure.</param>
		public static void Save(string connectionString, params object[] args)
		{
			DataAccess.ExecuteNonQuery(connectionString, "UpdateAdminHRDepartment", DataAccess.BuildSqlParameterArray(
				"@hrDepartmentID", SqlDbType.Int, args[0],
				"@hrDepartmentManagementTypeId", SqlDbType.Int, args[1]));
		}
		#endregion
	}
}
